﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Pre-Dies Actions Dialog
	This dialog will ask player to Upgrade property to some level
*/



package monopoly.GameController.Dialogs {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	import fl.controls.*;

	import monopoly.GameController.Dialogs.IDialog;
	import monopoly.GameController.Dialogs.Templates.*;
	import monopoly.Field.Field;
	import monopoly.Player.Player;
	import monopoly.IFS.GLOBALS;
	import monopoly.Localization.*;
	
	public class UpgradeDialog extends Sprite implements IDialog
		{
		public static const ALREADY_MAX_LEVEL:int	= 99;
		public static const NO_MONEY_TO_UPGRADE:int	= 0;
		public static const PASS:int				= 100;
		
		protected var _field:Field;
		protected var _chosen_action:uint = 0;		//IDialog implementation
		protected var _rdialog:RadiosDialog;
		protected var _rbuttons:Array = new Array( );
		protected var _possible_levels:Array = new Array( );
		protected var _myStrings:ILocalLanguage = LocalLanguage.getInstance();
		
		public function UpgradeDialog(field:Field)
			{
			_field = field;
			}
		public function loadDialog():void
			{
			var levels:int = calc_levels();
			
			if (NO_MONEY_TO_UPGRADE == levels || ALREADY_MAX_LEVEL == levels)
				{
				_chosen_action = levels;
				/* signal game controller we are done */
				dispatchEvent(new Event(Event.COMPLETE));
				return;
				}
			
			build_dialog(levels);
			}
		
		public function get chosenAction():uint
			{
			return _chosen_action;
			}
		public function get Data():Field
			{
			return _field;
			}
			
		/* protected area */
		
		protected function calc_levels():int
			{
			/* Max case */
			
			var curr_level:int = _field.UpgradeLevel;
			
			if (GLOBALS.UPGRADE_LEVELS == curr_level)
				return ALREADY_MAX_LEVEL;
			
			var p_cash:Number = _field.Owner.Cash;
			var upgrade_cost:Number;
			var curr_cost:Number = _field.Cost;
			
			trace("[UpgradeDialog] field cost: " + curr_cost);

			for (var level:int = curr_level; level <= GLOBALS.UPGRADE_LEVELS; level++)
				{
				upgrade_cost = Math.round(curr_cost * GLOBALS.UPGRADE_PERCENT);
				if (p_cash < upgrade_cost)
					{
					if (level == curr_level)
						return NO_MONEY_TO_UPGRADE;
					break;
					}
				else
					{
					_rbuttons.push(_myStrings.DlgUpgradeLevel(level, upgrade_cost));
					_possible_levels.push(level);
					}
				
				curr_cost = curr_cost + upgrade_cost;
				}
			_rbuttons.push(_myStrings.DlgUpgradePass);
			return level;
			}
		protected function build_dialog(levels:int):void
			{
			// need to add RadioDialog here
			var upgradeText:String = _myStrings.DlgUpgradeText(_field.PropertyName);
			_rdialog = new RadiosDialog(_myStrings.DlgUpgradeLabel, upgradeText, _rbuttons);
			_rdialog.addEventListener(MouseEvent.CLICK, onDialogComplete);
			addChild(_rdialog);
			}
		protected function onDialogComplete(e:MouseEvent):void
			{
			if (e.target is Button)
				{
				var dlg:RadiosDialog = e.currentTarget as RadiosDialog;
				var ChosenOption:uint = dlg.ChosenOption;
				
				/* check client has chosen something */
				
				if (BasicDialog.NO_CHOICE == dlg.ChosenOption)
					return;
				
				if (ChosenOption > _possible_levels.length-1)
					_chosen_action = PASS;
				else
					_chosen_action = _possible_levels[ChosenOption];
					
				e.currentTarget.removeEventListener(MouseEvent.CLICK, onDialogComplete);
				e.currentTarget.remove( );
				removeChild(Sprite(e.currentTarget));
				
				/* signal game controller we are done */

				dispatchEvent(new Event(Event.COMPLETE));
				}
			}
		
		}
}